package org.origin.centre.model.result;

import io.swagger.v3.oas.annotations.media.Schema;
import org.origin.centre.support.base.interfaces.XResult;
import org.springframework.http.HttpStatus;

/**
 * 数据返回实体类
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
@Schema(description = "响应实体类")
public class R<T> implements XResult {
    @Schema(description = "响应码", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer status;
    @Schema(description = "响应信息", accessMode = Schema.AccessMode.READ_ONLY, requiredMode = Schema.RequiredMode.REQUIRED, example = "成功或失败")
    private String message;
    @Schema(description = "响应数据", accessMode = Schema.AccessMode.READ_ONLY)
    private T data;

    public R() {
    }

    public R(XResult result) {
        this(result, null);
    }

    public R(XResult result, T data) {
        this(result.getStatus(), result.getMessage(), data);
    }

    public R(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    @Override
    public Integer getStatus() {
        return this.status;
    }

    public R<T> setStatus(Integer status) {
        this.status = status;
        return this;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    public R<T> setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static <T> R<T> of(XResult result, T data) {
        return new R<>(result, data);
    }

    public static <T> R<T> of(Integer status, String message, T data) {
        return new R<>(status, message, data);
    }

    public static <T> R<T> ok(XResult result) {
        return new R<>(result, null);
    }

    public static <T> R<T> ok(String message, T data) {
        return new R<>(HttpStatus.OK.value(), message, data);
    }

    public static <T> R<T> ok(XResult result, T data) {
        return new R<>(result, data);
    }

    public static <T> R<T> ok(String message) {
        return new R<>(HttpStatus.OK.value(), message, null);
    }

    public static <T> R<T> fail(XResult result) {
        return new R<>(result, null);
    }

    public static <T> R<T> fail(XResult result, T data) {
        return new R<>(result, data);
    }

    public static <T> R<T> fail(String message, T data) {
        return new R<>(HttpStatus.BAD_REQUEST.value(), message, data);
    }

    public static <T> R<T> fail(String message) {
        return new R<>(HttpStatus.BAD_REQUEST.value(), message, null);
    }
}
